package guru.merlin.server;

import guru.merlin.core.CantHandException;
import guru.merlin.core.Envelope;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author Cesar Obach
 *
 */
public class Nodes
{
	public Map nodes;
        
	public Nodes(){
		nodes=new HashMap();                
	}

	/**
	 * @param alias
	 * @param processor
	 * @return
	 */
	public int put(String alias,EPContainer container){
		if(nodes.get(alias)==null){
			nodes.put(alias,container);
			return 0;
		}
		else{
			return -1;
		}

	}

	/**
	 * @param alias
	 * @return
	 */
	public EPContainer get(String alias){
		return((EPContainer)nodes.get(alias));
	}

	/**
	 * @return
	 */
	public int size(){
		return(nodes.size());
	}

	/**
	 * 
	 */
	public void stopAll() throws Exception{
		Iterator iter;
		for(iter=nodes.values().iterator();iter.hasNext();){
			((EPContainer)iter.next()).stop();
		}
	}

	/**
	 * @param envelope
	 * @throws CantHandException
	 * @throws EPExecutionException
	 */
	public void sendToAll(Envelope envelope) throws CantHandException{
		Iterator iter;
		for(iter=nodes.values().iterator();iter.hasNext();){
			((EPContainer)iter.next()).handEnvelope(envelope);
		}
	}

}